﻿using System;
using Project_Maverick.Content.Images.Background;
using Project_Maverick.Controller.Input;
using Project_Maverick.Screen;
using Project_Maverick.Screen.GameScreen.ChatRoom;
using Project_Maverick.Screen.GameScreen.Loading;
using Project_Maverick.Screen.GameScreen.Play;
using Project_Maverick.Screen.GameScreen.Play.Map;
using Project_Maverick.Screen.Lobby.GameLobby;
using Project_Maverick.Screen.Lobby.SelectGameMode;
using Project_Maverick.Screen.Lobby.WaitingJoin;
using Project_Maverick.Screen.Playscreen;
using Project_Maverick.Screen.Playscreen.Character;
using Project_Maverick.Screen.Splash;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;

namespace Project_Maverick.GameRoot
{
    public class ProjectMaverick : Container
    {
        public enum Screen
        {
            None,
            Splash,
            ConfigureKeys,
            GameLobby,
            Leave,
            WaitingJoin,
            ServerConfigSelectMap,
            ServerConfigSelectMode,
            ServerConfigSelectName,
            CreateGame,
            ChatRoom,
            Loading,
            Play,
            Score,
            Replay,
            SelectGameMode
        }

        private Screen _previousScreen;
        private BaseScreen _currentScreen;

        private InputManager _inputManager;

        private Controller.Controller _gameController;

        public override void LoadContent()
        {
            SpriteSheetBackground.LoadContent();

            _inputManager = new InputManager();
            _inputManager.OnInputReceived += SendInputToScreen;

            _previousScreen = Screen.None;
            ChangeScreen(Screen.Loading, true);
            base.LoadContent();

            _gameController = new Controller.Controller();
        }

        void SendInputToScreen(PlayerInputCommand playerInputCommand)
        {
            if (playerInputCommand.Pressed)
            {
                _currentScreen.ReceiveLocalInputPressed(playerInputCommand);
            }
            else
            {
                _currentScreen.ReceiveLocalInputReleased(playerInputCommand);
            }
        }

        void ChangeScreen(Screen nextScreen, bool forward)
        {
            switch (_previousScreen)
            {
                case Screen.None:
                    break;
                case Screen.Splash:
                    break;
                case Screen.ConfigureKeys:
                    break;
                case Screen.GameLobby:
                    break;
                case Screen.Leave:
                    break;
                case Screen.WaitingJoin:
                    break;
                case Screen.ServerConfigSelectMap:
                    break;
                case Screen.ServerConfigSelectMode:
                    break;
                case Screen.ServerConfigSelectName:
                    break;
                case Screen.CreateGame:
                    break;
                case Screen.ChatRoom:
                    break;
                case Screen.Loading:
                    _gameController.Maps = (_currentScreen as Loading).Maps;
                    _gameController.MapName = (_currentScreen as Loading).MapName;
                    _gameController.AddPlayer(0, new Character(0, new Team(0, "Team 0", Color.Red)), PlayerInputCommand.SourceType.Local);
                    break;
                case Screen.Play:
                    break;
                case Screen.Score:
                    break;
                case Screen.Replay:
                    break;
                case Screen.SelectGameMode:
                    break;
            }

            if (_currentScreen != null)
            {
                _currentScreen.Exit(forward);
                RemoveEvents();
            }

            switch (nextScreen)
            {
                case Screen.Splash:
                    _currentScreen = new Splash();
                    break;
                case Screen.Leave:
                    break;
                case Screen.ConfigureKeys:
                    break;
                case Screen.SelectGameMode:
                    _currentScreen = new SelectGameMode();
                    break;
                case Screen.GameLobby:
                    _currentScreen = new GameLobby();
                    break;
                case Screen.WaitingJoin:
                    _currentScreen = new WaitingJoin("LOCALHOST");
                    break;
                case Screen.ServerConfigSelectMap:
                    break;
                case Screen.ServerConfigSelectMode:
                    break;
                case Screen.ServerConfigSelectName:
                    break;
                case Screen.CreateGame:
                    break;
                case Screen.ChatRoom:
                    _currentScreen = new ChatRoom();
                    break;
                case Screen.Loading:
                    _currentScreen = new Loading(Map.MapNames.Test);
                    break;
                case Screen.Play:
                    _currentScreen = new Playscreen(_gameController);
                    break;
                case Screen.Score:
                    break;
                case Screen.Replay:
                    break;
            }

            if(nextScreen != Screen.Splash)
                _currentScreen.Enter(forward);
            AddChildren(_currentScreen);
            AddEvents();

            _previousScreen = nextScreen;
        }

        void RemoveEvents()
        {
            _currentScreen.OnChangeScreen -= ChangeScreen;
        }

        void AddEvents()
        {
            _currentScreen.OnChangeScreen += ChangeScreen;
        }
    }
}